/* 
 *	Copyright (C) 2009-2010 miaohua
 *	http://code.google.com/p/lrcshower/
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *   
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *   
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *  http://www.gnu.org/copyleft/gpl.html
 *
 */

// MusicButton.cpp : implementation file
//

#include "stdafx.h"
#include "MusicBox.h"
#include "MenuButton.h"
#include "MFCHelperFuncs.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

// CMusicButton

IMPLEMENT_DYNAMIC(CMenuButton, CMFCButton)

BEGIN_MESSAGE_MAP(CMenuButton, CMFCButton)
    //{{AFX_MSG_MAP(CMusicButton)
    ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()



// CMusicButton message handlers
void CMenuButton::OnMouseMove(UINT nFlags, CPoint Point) 
{ 

}


void CMenuButton::OnLButtonDown(UINT nFlags, CPoint Point) 
{
     CPoint Pos = Point;
     ClientToScreen(&Pos);
     CRect Rect;
     GetWindowRect(Rect);
     if( Rect.PtInRect(Pos) )
     {	
         CMenu* Popup = m_Menu.GetSubMenu(0);
         ASSERT( Popup != NULL );
         Popup->TrackPopupMenu(TPM_LEFTALIGN|TPM_LEFTBUTTON,Rect.left,Rect.bottom,GetParent());
     }
}


BOOL CMenuButton::LoadMenu(UINT MenuID)
{
     return m_Menu.LoadMenu(MenuID);
}

void CMenuButton::SetTip(UINT TipID)
{
     CString Tip;
     VERIFY(Tip.LoadString(TipID) == TRUE);
     SetTip(Tip);
}

void CMenuButton::SetTip(CString  Tip)
{
     VERIFY(RegisterTipTool() == TRUE);
     m_TipCtl.UpdateTipText(Tip, this);
}

void CMenuButton::CheckMenuRadioItem(UINT FirstMenuID, UINT LastMenuID, UINT CheckMenuID)
{
     m_Menu.CheckMenuRadioItem(FirstMenuID, LastMenuID, CheckMenuID, MF_BYCOMMAND);
}

void CMenuButton::CheckMenuItem(LONG MenuItem, bool Val)
{
     CheckItem(&m_Menu, MenuItem, Val);
}

bool CMenuButton::GetCheckMenuItemState(LONG MenuItem)
{
     // Get submenu check state
     MENUITEMINFO MenuInfo;
     memset(&MenuInfo, 0, sizeof(MenuInfo));
     MenuInfo.cbSize = sizeof(MenuInfo);
     MenuInfo.fMask  = MIIM_STATE;
     m_Menu.GetMenuItemInfo(MenuItem, &MenuInfo);
     return (MenuInfo.fState & MFS_CHECKED) != 0;
}

BOOL CMenuButton::RegisterTipTool() 
{ 
     // If the button hasn't been created, then just return
     if( !GetSafeHwnd() ) 
         return FALSE ;       
        
     if( !m_TipCtl.GetSafeHwnd() ) 
         m_TipCtl.Create(GetParent()) ; 
     else 
         return TRUE;
        
     m_TipCtl.DelTool(this); 
     m_TipCtl.AddTool(this); 

     return TRUE ; 
} 

BOOL CMenuButton::PreTranslateMessage(MSG* pMsg) 
{ 
     if( m_TipCtl.GetSafeHwnd() ) 
         m_TipCtl.RelayEvent(pMsg) ; 

     return CMFCButton::PreTranslateMessage(pMsg); 
}
